package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.*;
import com.zbl.zblproject.core.mapper.*;
import com.zbl.zblproject.core.mapper.ChinaMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.erp.entity.ErpOrder;
import com.zbl.zblproject.erp.mapper.ErpOrderMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/31
 */
@Api(tags = "门店接口")
@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private ErpOrderMapper erpOrderMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private ChinaMapper chinaMapper;

    @GetMapping("/one/{info}")
    @ApiOperation("根据订单号获取门店信息和订单数据")
    public Result getOneByOrderInfo(@ApiParam("订单号") @PathVariable String info) {
        QueryWrapper<ErpOrder> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("order_info", info);
        ErpOrder order = erpOrderMapper.selectOne(orderWrapper);
        if (order == null) {
            return ResultUtil.error("没有发现订单号!");
        }

        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.select("user_id", "user_name", "logistics", "telephone").eq("user_id", order.getStoreId());
        AdminUser user = adminUserMapper.selectOne(wrapper);

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_sn", order.getOriginOrderSn());
        Order originOrder = orderMapper.selectOne(orderQueryWrapper);
        if (originOrder == null) {
            return ResultUtil.error("该订单没有原始订单!");
        }

        UserAddress userAddress = userAddressMapper.selectById(originOrder.getAddressId());

        JSONObject object = new JSONObject();
        object.put("user", user);
        object.put("orderSn", order.getOriginOrderSn());
        object.put("time", order.getExecuteTime());
        object.put("telephone", user.getTelephone());

        //收货地址
        object.put("deliveryAddress", user.getAddress());
        object.put("total", erpOrderMapper.selectOrderTotal(order.getOrderId()));
        //托运地址
        if (userAddress == null) {
            object.put("shippingAddress", "");
            object.put("area", "");
            object.put("mobile", "");
        } else {
            object.put("shippingAddress", userAddress.getAddress());
            China province =chinaMapper.selectById(userAddress.getProvince());
            China city = chinaMapper.selectById(userAddress.getCity());
            China district = chinaMapper.selectById(userAddress.getDistrict());
            String provinceStr = province==null?"":province.getName();
            String cityStr = city==null?"":city.getName();
            String districtStr = district==null?"":district.getName();
            String area = provinceStr + cityStr + districtStr;
//            String area = regionMapper.selectById(userAddress.getDistrict()).getRegionName();
            object.put("area", area);
            object.put("mobile", userAddress.getMobile());
        }
        //指定物流
        object.put("logistics", user.getLogistics());
        return ResultUtil.success("成功!", object);
    }


    @GetMapping("/two/{info}")
    @ApiOperation("根据订单号获取门店信息和订单数据")
    public Result getOneByOrder(@ApiParam("订单号") @PathVariable String info) {
        QueryWrapper<ErpOrder> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("order_info", info);
        ErpOrder order = erpOrderMapper.selectOne(orderWrapper);
        if (order == null) {
            return ResultUtil.error("没有发现订单号!");
        }

        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.select("user_id", "user_name", "logistics", "telephone").eq("user_id", order.getStoreId());
        AdminUser user = adminUserMapper.selectOne(wrapper);

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_sn", order.getOriginOrderSn());
        Order originOrder = orderMapper.selectOne(orderQueryWrapper);
        if (originOrder == null) {
            return ResultUtil.error("该订单没有原始订单!");
        }

        UserAddress userAddress = userAddressMapper.selectById(originOrder.getAddressId());

        JSONObject object = new JSONObject();
        object.put("user", user);
        object.put("orderSn", order.getOriginOrderSn());
        object.put("time", order.getExecuteTime());
        object.put("telephone", user.getTelephone());

        //收货地址
        object.put("deliveryAddress", user.getAddress());
        object.put("total", erpOrderMapper.selectOrderTotal(order.getOrderId()));
        //托运地址
        if (userAddress == null) {
            object.put("shippingAddress", "");
            object.put("area", "");
            object.put("mobile", "");
        } else {
            object.put("shippingAddress", userAddress.getAddress());
            String area = chinaMapper.selectById(userAddress.getProvince()).getName() + chinaMapper.selectById(userAddress.getCity()).getName() + chinaMapper.selectById(userAddress.getDistrict()).getName();
//            String area = regionMapper.selectById(userAddress.getDistrict()).getRegionName();
            object.put("area", area);
            object.put("mobile", userAddress.getMobile());
        }
        //指定物流
        object.put("logistics", user.getLogistics());
        return ResultUtil.success("成功!", object);
    }

    @GetMapping("/all")
    @ApiOperation("获取全部用户信息")
    public Result list() {
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.select("user_id", "user_name");
        List<AdminUser> list = adminUserMapper.selectList(wrapper);
        return ResultUtil.success("成功", list);
    }

}
